package com.android.support.lib.universal.view.gamepad;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

import com.android.support.lib.universal.R;
import com.android.support.lib.universal.listenner.VirtualKeyListener;

/**
 * A , B , X , Y 四个功能键
 *
 * @author Administrator
 */
public class FuncKeyView extends View {

    private VirtualButton mBtnA;
    private VirtualButton mBtnB;
    private VirtualButton mBtnX;
    private VirtualButton mBtnY;

    private int mWidth;
    private int mHeight;
    //大按钮B的直径
    private int mBigBtnRadius;
    //小按钮的直径
    private int mSmallBtnRadius;

    //位图偏移左边的位置
    private int paddingLeft = 0;
    //位图偏移顶部的位置
    private int paddingTop = 0;

    //按钮高度
    int mBtnHeight;
    //B键按钮高度
    int mBtnBHeight;
    //按键坐标
    int mBtnY_X;
    int mBtnY_Y;

    int mBtnX_X;
    int mBtnX_Y;

    int mBtnA_X;
    int mBtnA_Y;

    int mBtnB_X;
    int mBtnB_Y;

    private VirtualKeyListener mVirtualKeyListener;

    private Paint mPaint;

    public FuncKeyView(Context context) {
        this(context, null);
    }

    public FuncKeyView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FuncKeyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint();
    }

    //设置功能按键监听事件
    public void setVirtualKeyListener(VirtualKeyListener virtualKeyListener) {
        mVirtualKeyListener = virtualKeyListener;
    }

    @Override
    public void layout(int left, int top, int right, int bottom) {

        int width = right - left;
        int height = bottom - top;

        width = width > height ? height : width;
        measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY));

        if (mBtnA == null) {
            mWidth = right - left;
            mHeight = bottom - top;

            //按钮大小
            mBigBtnRadius = (int) Math.round(mWidth / 2.4);
            mSmallBtnRadius = (int) Math.round(mWidth / 3);

            mBtnBHeight = mBigBtnRadius;
            mBtnHeight = mSmallBtnRadius;

            //按钮位置
            mBtnY_X = mWidth + paddingLeft - mSmallBtnRadius;
            mBtnY_Y = paddingTop;

            mBtnX_X = mWidth / 2 - (mSmallBtnRadius * 3 / 4);
            mBtnX_Y = mHeight / 2 - (mSmallBtnRadius * 3 / 4);

            mBtnA_X = paddingLeft + (mBtnHeight / 10);
            mBtnA_Y = mHeight - paddingTop - mSmallBtnRadius;

            mBtnB_X = mWidth + paddingLeft - mBigBtnRadius;
            mBtnB_Y = mHeight - paddingTop - (mBtnBHeight * 13 / 12);

            if (isInEditMode()) {
                return;
            }
            //按钮样式
            Resources res = getResources();
            mBtnY = new VirtualButton(Bitmap.createScaledBitmap(
                    BitmapFactory.decodeResource(res, R.drawable.lib_gamepad_default_y),//默认样式
                    mSmallBtnRadius, mSmallBtnRadius, true),
                    Bitmap.createScaledBitmap(
                            BitmapFactory.decodeResource(res, R.drawable.lib_gamepad_center_y),//点击样式
                            mSmallBtnRadius, mSmallBtnRadius, true), mBtnY_X,
                    mBtnY_Y, GamepadConfig.KEY_CODE_Y, mSmallBtnRadius);
            mBtnX = new VirtualButton(Bitmap.createScaledBitmap(
                    BitmapFactory.decodeResource(res, R.drawable.lib_gamepad_default_x),
                    mSmallBtnRadius, mSmallBtnRadius, true),
                    Bitmap.createScaledBitmap(
                            BitmapFactory.decodeResource(res, R.drawable.lib_gamepad_center_x),
                            mSmallBtnRadius, mSmallBtnRadius, true), mBtnX_X,
                    mBtnX_Y, GamepadConfig.KEY_CODE_X, mSmallBtnRadius);
            mBtnA = new VirtualButton(Bitmap.createScaledBitmap(
                    BitmapFactory.decodeResource(res, R.drawable.lib_gamepad_default_a),
                    mSmallBtnRadius, mSmallBtnRadius, true),
                    Bitmap.createScaledBitmap(
                            BitmapFactory.decodeResource(res, R.drawable.lib_gamepad_center_a),
                            mSmallBtnRadius, mSmallBtnRadius, true), mBtnA_X,
                    mBtnA_Y, GamepadConfig.KEY_CODE_A, mSmallBtnRadius);
            mBtnB = new VirtualButton(Bitmap.createScaledBitmap(
                    BitmapFactory.decodeResource(res, R.drawable.lib_gamepad_default_b),
                    mBigBtnRadius, mBigBtnRadius, true),
                    Bitmap.createScaledBitmap(
                            BitmapFactory.decodeResource(res, R.drawable.lib_gamepad_center_b),
                            mBigBtnRadius, mBigBtnRadius, true), mBtnB_X,
                    mBtnB_Y, GamepadConfig.KEY_CODE_B, mBigBtnRadius);
        }
        super.layout(left, top, right, bottom);
    }

    //按键事件
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        mBtnA.dispatchTouchEvent(event);
        mBtnB.dispatchTouchEvent(event);
        mBtnX.dispatchTouchEvent(event);
        mBtnY.dispatchTouchEvent(event);
        invalidate();
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (isInEditMode()) {
            return;
        }
        canvas.drawBitmap(mBtnA.getBitmap(), mBtnA.getX(), mBtnA.getY(), mPaint);
        canvas.drawBitmap(mBtnB.getBitmap(), mBtnB.getX(), mBtnB.getY(), mPaint);
        canvas.drawBitmap(mBtnX.getBitmap(), mBtnX.getX(), mBtnX.getY(), mPaint);
        canvas.drawBitmap(mBtnY.getBitmap(), mBtnY.getX(), mBtnY.getY(), mPaint);
        super.onDraw(canvas);
    }

    class KeyEventTask extends Thread {

        private VirtualButton mButton;

        public KeyEventTask(VirtualButton button) {
            mButton = button;
        }

        @Override
        public void run() {
            while (mButton.getBKeyDown()) {

                if (mVirtualKeyListener != null) {
                    post(new Runnable() {
                        @Override
                        public void run() {
                            mVirtualKeyListener
                                    .onKeyEvent(new KeyEvent(
                                            KeyEvent.ACTION_DOWN, mButton
                                            .getKeyCode()));
                        }
                    });
                }
                try {
                    Thread.sleep(40);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            if (mVirtualKeyListener != null) {
                post(new Runnable() {
                    @Override
                    public void run() {
                        mVirtualKeyListener.onKeyEvent(new KeyEvent(
                                KeyEvent.ACTION_UP, mButton.getKeyCode()));
                    }
                });
            }
        }
    }

    public class VirtualButton {
        private int defaultID = -1;
        private int id = defaultID;
        private Bitmap mCurrentBitmap;
        private Bitmap mNormalBitmap;
        private Bitmap mSelectedBitmap;
        private int VX;
        private int VY;
        private int VX_End;
        private int VY_End;
        private int mKeyCode;

        private boolean BKeyDown = false;

        public VirtualButton(Bitmap normalBitmap, Bitmap selectedBitmap, int x,
                             int y, int keycode, int Radius) {
            mNormalBitmap = normalBitmap;
            mSelectedBitmap = selectedBitmap;
            mCurrentBitmap = mNormalBitmap;
            mKeyCode = keycode;
            VX = x;
            VY = y;
            VX_End = VX + Radius;
            VY_End = VY + Radius;
        }

        public int getX() {
            return VX;
        }

        public int getY() {
            return VY;
        }

        public Bitmap getBitmap() {
            return mCurrentBitmap;
        }

        public boolean getBKeyDown() {
            return BKeyDown;
        }

        public int getKeyCode() {
            return mKeyCode;
        }

        public void keyDown(int id) {
            if (this.id == defaultID) {
                this.id = id;
                BKeyDown = true;
                new KeyEventTask(this).start();
                mCurrentBitmap = mSelectedBitmap;
                //震动音效
                GamepadConfig.STYLE();
            }
        }

        public void keyUp(int id) {
            if (BKeyDown && id != defaultID && id == this.id) {
                BKeyDown = false;
                mCurrentBitmap = mNormalBitmap;
                this.id = defaultID;
            }
        }

        public void dispatchTouchEvent(MotionEvent event) {
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_POINTER_DOWN:
                case MotionEvent.ACTION_DOWN:
                case MotionEvent.ACTION_MOVE:
                    for (int i = 0; i < event.getPointerCount(); i++) {
                        float x = event.getX(i);
                        float y = event.getY(i);
                        if (x > VX && x < VX_End && y > VY && y < VY_End) {
                            keyDown(event.getPointerId(i));
                        } else {
                            keyUp(event.getPointerId(i));
                        }
                    }
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    keyUp(event.getPointerId(event.getActionIndex()));
                    break;
            }
        }
    }
}
